package edu.dayu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.dayu.pojo.QrtzCronTriggers;
import edu.dayu.service.JobService;
import edu.dayu.service.QrtzCronTriggersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/job")
@Slf4j
public class JobController {
    @Autowired
    private JobService jobService;

    @Autowired
    private QrtzCronTriggersService qrtzCronTriggersService;

    @PostMapping("/addJob")
    public String addJob(@RequestParam(value="jobClassName")String jobClassName,
                       @RequestParam(value="jobGroupName")String jobGroupName,
                       @RequestParam(value="cronExpression")String cronExpression){
        boolean flag = jobService.addJob(jobClassName, jobGroupName, cronExpression);
        return flag?"成功":"失败";
    }

    @PostMapping("/pauseJob")
    public String pauseJob(@RequestParam(value="jobClassName")String jobClassName,
                         @RequestParam(value="jobGroupName")String jobGroupName){
        boolean flag = jobService.pauseJob(jobClassName, jobGroupName);
        return flag?"成功":"失败";
    }

    @PostMapping("/resumeJob")
    public String resumeJob(@RequestParam(value="jobClassName")String jobClassName,
                         @RequestParam(value="jobGroupName")String jobGroupName){
        boolean flag = jobService.resumeJob(jobClassName, jobGroupName);
        return flag?"成功":"失败";
    }

    @PostMapping("/rescheduleJob")
    public String rescheduleJob(@RequestParam(value="jobClassName")String jobClassName,
                         @RequestParam(value="jobGroupName")String jobGroupName,
                         @RequestParam(value="cronExpression")String cronExpression){
        boolean flag = jobService.rescheduleJob(jobClassName, jobGroupName, cronExpression);
        return flag?"成功":"失败";
    }

    @PostMapping("/deleteJob")
    public String deleteJob(@RequestParam(value="jobClassName")String jobClassName,
                         @RequestParam(value="jobGroupName")String jobGroupName){
        boolean flag = jobService.deleteJob(jobClassName, jobGroupName);
        return flag?"成功":"失败";
    }

    @PostMapping("/queryJobAll")
    public List<QrtzCronTriggers> qrtzCronTriggersList(){
        BaseMapper<QrtzCronTriggers> baseMapper = qrtzCronTriggersService.getBaseMapper();
        return baseMapper.selectList(new QueryWrapper<>());
    }

}
